package com.bd.learning.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelFill {

    public static void main(String[] args) {
        String source = "D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\t2.xlsx";
        String sink = "D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\剩余接口.xlsx";
        ExcelFill ef = new ExcelFill();
        List<BeanObj> list = ef.getBeanObj(source,0);
        Map<String, String> map = ef.getMap(source, 1);
        int num = 0;
        for(BeanObj b : list){
            num++;
            if(num != 1) {
                if(StrUtil.isEmpty(b.getServerName())){
                    b.setServerName(map.get(b.getPackageName()));
                    System.out.println(b);
                }
            }
        }

        ExcelWriter writer = ExcelUtil.getWriter(sink);
        writer.write(list,false);
        writer.close();
    }

    public List<BeanObj> getBeanObj(String file, int index){
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(file),index);
        List<List<Object>> readAll = reader.read();
        List<BeanObj> totalList = new ArrayList<>();
        for (List<Object> list : readAll){
            BeanObj bo = new BeanObj();
            bo.setType1((String) list.get(0));
            bo.setType2((String) list.get(1));
            bo.setPackageName(String.valueOf(list.get(2)));
            bo.setFileName(String.valueOf(list.get(3)));
            bo.setMethodName(String.valueOf(list.get(4)));
            bo.setMethodNum(String.valueOf(list.get(5)));
            bo.setServerName(String.valueOf(list.get(6)));
            totalList.add(bo);
        }
        reader.close();
        return totalList;
    }


    public Map<String,String> getMap(String file, int index){
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(file),index);
        List<List<Object>> readAll = reader.read();
        Map<String,String> map = new HashMap<>();
        for (List<Object> list : readAll){
            map.put(String.valueOf(list.get(0)),String.valueOf(list.get(2)));
        }
        return map;
    }


}
